<?php

namespace app\common\business;

use app\common\model\mysql\SDistribute as SDistributeModel;

class SDistribute
{
    public $distributeObj = null;
    public function __construct()
    {
        $this->distributeObj = new SDistributeModel();
    }
    /**
     * 分发平台查询
     * @param $where
     * @param $field
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getNormalDistribute($where,$field,$limit,$page){
        $dictData = $this->distributeObj->getNormalDistributeList($where,$field,$limit,$page);
        if(!$dictData){
            return [];
        }
        $dictData = $dictData->toArray();
        return $dictData;
    }

    //添加分发平台
    public function addDistribute($data){
        try {
            //根据$config_name去数据库查询是否存在这条记录
            $postNameResult = $this->getDistributeByName($data['distribute_name']);
            if($postNameResult){
                throw new \think\Exception("该分发平台名称已经存在请重新设置");
            }
            $this->distributeObj->save($data);
        }catch (\Exception $e){
            throw new \think\Exception($e->getMessage());
        }
        return $this->distributeObj->id;
    }

    public function getDistributeByName($distribute_name,$distribute_id = ''){
        $user = $this->distributeObj->getDistributeByNameModel($distribute_name,$distribute_id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    //行业类别修改
    public function updateDistributeById($id,$data){
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }
        $postNameResult = $this->getDistributeByName($data['distribute_name'],$id);
        if($postNameResult){
            throw new \think\Exception("该分发平台名称已经存在请重新设置");
        }
        $where = [
            "distribute_id"=>$id,
        ];
        return $this->distributeObj->where($where)->save($data);
    }

    //根据$id,去数据库查询是否存在这条记录
    public function getDistributeById($id){
        $user = $this->distributeObj->getDistributeIdData($id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    //根据$ids数组，删除数据库记录delete
    public function delDistributeIds($ids){
        $ids = explode(',', $ids);
        $result = $this->distributeObj->delDistributeIds($ids);
        return $result;
    }
}
